var laypage = layui.laypage;

var pageNumber = getQueryString("pageNumber") || 1;
var pageSize = getQueryString("pageSize") || 14;

document.title = "t-io赞赏统计！";


var table = layui.table;


/**
 * 按赞赏渠道总额统计
 */
function statAmountComeFrom() {
    ajax.get("/donate/statAmountComeFrom", {
        data: {
            // "pageNumber": pageNumber,
            // "pageSize": pageSize
        },
        success: function (resp) {
            var data = resp.data;

            var allsum = 0;
            var allcount = 0;
            for (var i = 0; i < data.length; i++) {
                allsum += data[i].sum;
                allcount += data[i].count;
            }
            data.push({ sum: allsum, count: allcount, way: "汇总", waytype: 99 });

            log(data);

            //第一个实例
            table.render({
                elem: '#statAmountComeFrom'
                // , height: 315
                // , url: '/demo/table/user/' //数据接口
                , data: data
                // , page: true //开启分页
                , cols: [[ //表头
                    {
                        field: 'way', title: '渠道', templet: function (d) {
                            var way_html = "";
                            if (true || d.waytype) {
                                var xx = res_url('/img/donate_way/' + d.waytype + '.jpg');
                                way_html = '<img class="layui-anim layui-anim-scaleSpring" alt="true" src="'+ xx +'" style="width: 25px; height: 25px; border-radius: 50%; margin-right:10px;">';
                            }

                            if (d.waytype == 99) {
                                return way_html + "<span style='font-weight:bold'>" + d.way + "</span>";
                            } else {
                                return way_html + d.way;
                            }
                        }
                    }
                    , {
                        field: 'sum', title: '总额', templet: function (d) {
                            return number_format(d.sum, 2, ".", ",");//1,234,567.09
                        }
                    }
                    , { field: 'count', title: '次数' }

                ]]
            });
        }
    });
}



/**
 * 按赞赏渠道次数统计
 */
function statCountComeFrom() {
    ajax.get("/donate/statCountComeFrom", {
        data: {
            // "pageNumber": pageNumber,
            // "pageSize": pageSize
        },
        success: function (resp) {
            var data = resp.data;

            var allsum = 0;
            var allcount = 0;
            for (var i = 0; i < data.length; i++) {
                allsum += data[i].sum;
                allcount += data[i].count;
            }
            data.push({ sum: allsum, count: allcount, way: "汇总", waytype: 99 });

            log(data);

            //第一个实例
            table.render({
                elem: '#statCountComeFrom'
                // , height: 315
                // , url: '/demo/table/user/' //数据接口
                , data: data
                // , page: true //开启分页
                , cols: [[ //表头
                    {
                        field: 'way', title: '渠道', templet: function (d) {
                            var way_html = "";
                            if (true || d.waytype) {
                                var xx = res_url('/img/donate_way/' + d.waytype + '.jpg');
                                way_html = '<img class="layui-anim layui-anim-scaleSpring" alt="true" src="'+xx+'" style="width: 25px; height: 25px; border-radius: 50%; margin-right:10px;">';
                            }

                            if (d.waytype == 99) {
                                return way_html + "<span style='font-weight:bold'>" + d.way + "</span>";
                            } else {
                                return way_html + d.way;
                            }
                        }
                    }
                    , { field: 'count', title: '次数' }
                    , {
                        field: 'sum', title: '总额', templet: function (d) {
                            return number_format(d.sum, 2, ".", ",");//1,234,567.09
                        }
                    }


                ]]
            });
        }
    });
}

/**
 * 按赞赏额统计
 */
function orderAmountByName() {
    ajax.get("/donate/orderAmountByName", {
        data: {
            // "pageNumber": pageNumber,
            // "pageSize": pageSize
        },
        success: function (resp) {
            var data = resp.data;

            log(data);

            //第一个实例
            table.render({
                elem: '#orderAmountByName'
                // , height: 315
                // , url: '/demo/table/user/' //数据接口
                , data: data
                // , page: true //开启分页
                , cols: [[ //表头
                    {
                        field: 'name', width: 250, title: '姓名', templet: function (d) {
                            if (!d.avatar) {
                                d.avatar = "/img/1.png";
                            }
                            var xx = res_url(d.avatar);
                            var avatar_html = '<img class="layui-anim layui-anim-scaleSpring" alt="true" src="' + xx + '" style="width: 25px; height: 25px; border-radius: 50%; margin-right:10px;">';
                            if (d.url) {
                                return "<a href='" + d.url + "' target='_blank'>" + avatar_html + d.name + "</a>";
                            } else {
                                return avatar_html + d.name;
                            }


                        },
                    }
                    , {
                        field: 'sum', title: '总额', templet: function (d) {
                            return number_format(d.sum, 2, ".", ",");//1,234,567.09
                        }
                    }
                    , { field: 'count', title: '次数' }

                ]]
            });
        }
    });
}



/**
 * 按赞赏次数统计
 */
function orderCountByName() {
    ajax.get("/donate/orderCountByName", {
        data: {
            // "pageNumber": pageNumber,
            // "pageSize": pageSize
        },
        success: function (resp) {
            var data = resp.data;

            log(data);

            //第一个实例
            table.render({
                elem: '#orderCountByName'
                // , height: 315
                // , url: '/demo/table/user/' //数据接口
                , data: data
                // , page: true //开启分页
                , cols: [[ //表头
                    {
                        field: 'name', width: 250, title: '姓名', templet: function (d) {
                            if (!d.avatar) {
                                d.avatar = "/img/1.png";
                            }
                            var xx = res_url(d.avatar);
                            var avatar_html = '<img class="layui-anim layui-anim-scaleSpring" alt="true" src="' + xx + '" style="width: 25px; height: 25px; border-radius: 50%; margin-right:10px;">';
                            if (d.url) {
                                return "<a href='" + d.url + "' target='_blank'>" + avatar_html + d.name + "</a>";
                            } else {
                                return avatar_html + d.name;
                            }


                        },
                    }
                    , { field: 'count', title: '次数' }
                    , {
                        field: 'sum', title: '总额', templet: function (d) {
                            return number_format(d.sum, 2, ".", ",");//1,234,567.09
                        }
                    }


                ]]
            });
        }
    });
}

var donateDuration = 10;
var donate_tip_wrap = document.getElementById("donate_tip_wrap");
/**
 * 找出最后一笔赞赏时间
 */
function selectLastDonate() {
    if (donate_tip_wrap) {
        log("没有id为donate_tip_wrap的元素");
        return;
    }

    ajax.get("/donate/selectLastDonate", {
        data: {
            // "pageNumber": pageNumber,
            // "pageSize": pageSize
        },
        success: function (resp) {
            var data = resp.data;

            var donate_tip = document.getElementById("donate_tip");
            // donate_tip.innerHTML = data.fromatedDuration;

            donateDuration = data.duration;
            // showFromatedDuration();


            if (donate_tip) {
                setInterval('showFromatedDuration()', 1000);
            }

        }
    });
}

function showFromatedDuration() {

    var date3 = donateDuration;//date2.getTime() - date1.getTime()  //时间差的毫秒数

    donateDuration += 1000;


    //计算出相差天数
    var days = Math.floor(date3 / (24 * 3600 * 1000))

    //计算出小时数

    var leave1 = date3 % (24 * 3600 * 1000)    //计算天数后剩余的毫秒数
    var hours = Math.floor(leave1 / (3600 * 1000))
    //计算相差分钟数
    var leave2 = leave1 % (3600 * 1000)        //计算小时数后剩余的毫秒数
    var minutes = Math.floor(leave2 / (60 * 1000))
    //计算相差秒数
    var leave3 = leave2 % (60 * 1000)      //计算分钟数后剩余的毫秒数
    var seconds = Math.round(leave3 / 1000)
    // alert(" 相差 " + days + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒")
    if (seconds < 10) {
        seconds = "0" + seconds;
    }
    donate_tip_wrap.style.display = "";
    donate_tip.innerHTML = days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒";
}


/**
 * 分页查询
 * @param {} pageNumber 
 * @param {*} pageSize 
 */
function page(pageNumber, pageSize) {
    table.render({
        elem: '#itemlist'
        // , skin: 'line' //行边框风格
        , height: 620
        , limit: pageSize
        , limits: layui_page_limits
        // , totalRow: true   //是否开启合计行区域。layui 2.4.0 新增
        , request: {
            pageName: 'pageNumber' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }

        , done: function (res, curr, count) {
            //如果是异步请求数据方式，res即为你接口返回的信息。
            //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
            // log("donedonedonedonedone", res);

            //得到当前页码
            // log(curr);

            //得到数据总量
            // log(count);
            layui_page_done(res.pageNumber, res.pageSize);
            //switchDonation();
        }
        // , height: 1500
        , url: path('/donate/page') //数据接口
        //, parseData
        , parseData: layui_page_parseData
        // , data: _data
        // , limit: _data.length
        , page: true //开启分页
        , cols: [[ //表头
            {
                field: 'name', width: 220, title: '姓名', sort: true, templet: function (d) {
                    if (!d.avatar) {
                        d.avatar = "/img/1.png";
                    }
                    var xx = res_url(d.avatar);
                    var avatar_html = '<img class="layui-anim layui-anim-scaleSpring" alt="true" src="' + xx + '" style="width: 25px; height: 25px; border-radius: 50%; margin-right:4px;">';
                    if (d.url) {
                        return "<a href='" + d.url + "' target='_blank'>" + avatar_html + d.name + "</a>";
                    } else {
                        return avatar_html + d.name;
                    }


                }
            },
            {
                field: 'way', width: 104, title: '渠道', sort: true, templet: function (d) {
                    var xx = res_url('/img/donate_way/' + d.waytype + '.jpg');
                    var way_html = '<img class="layui-anim layui-anim-scaleSpring" title="' + d.way + '" src="'+xx+'" style="width: 25px; height: 25px; border-radius: 50%; margin-right:4px;">';
                    if (d.way == '码云') {
                        // return d.way + "(含talent-aio时期的赞赏)";
                        return way_html + number_format(d.amount, 2, ".", ",");
                    } else {
                        return way_html + number_format(d.amount, 2, ".", ",");
                    }
                }
            },
            {
                field: 'leavemsg', title: '留言', templet: function (d) {
                    if (d.leavemsg) {
                        return d.leavemsg;
                        // return "密";
                    }
                    return "";

                }
            },
            {
                field: 'time', width: 160, title: '时间', sort: true, style: ""
            }
            // ,
            // {
            //     field: 'tag', width: 250, title: '备注', templet: function (d) {

            //         var str = '        <div class="tags___EMFBs">';
            //         if (d.tag1) {
            //             str += '            <span>' + d.tag1 + '</span>';
            //         }
            //         if (d.tag2) {
            //             str += '            <span>' + d.tag2 + '</span>';
            //         }
            //         if (d.tag3) {
            //             str += '            <span>' + d.tag3 + '</span>';
            //         }

            //         str += '        </div>';
            //         return str;
            //     }
            // }

        ]]
    });

    // var list = data;
    // var listEle = document.getElementById("list");
    // listEle.innerHTML = "";
    // if (list) {
    //     for (var i = 0; i < list.length; i++) {
    //         var e = list[i];
    //         var itemStr = renderItem(e);
    //         listEle.innerHTML += itemStr;
    //     }
    // }
}


statAmountComeFrom();
statCountComeFrom();
orderAmountByName();
orderCountByName()
page(pageNumber, pageSize);

selectLastDonate();


$(function () {
    //让右侧赞赏显示
    //document.getElementById("right_donate").style.display = "block";

    layui.$('#iWantDonation').on('mouseenter', function () {
        this.index = layer.tips('您的赞赏不会给t-io带来多大的物质支撑<br>却能让t-io社区显得更加朝气活泼<br><p style="text-align:right;">—— t-io</p>', this, { time: -1, maxWidth: 280, tips: [3, '#3A3D49'] });
        $(this).html('<i class="layui-icon layui-icon-face-smile-fine" style="font-size:35px;font-weight: 600;"></i>');
    }).on('mouseleave', function () {
        layer.close(this.index);
        $(this).html("赏");
    });

    $(this).addClass("on");

});

function switchDonation() {
    var ele = document.getElementById("donate_img_container"); 
    var iWantDonation = document.getElementById("iWantDonation");
    if (ele) {
        if (ele.style.visibility == "hidden") {
            ele.style.visibility = "";
            iWantDonation.innerHTML = "赏";

        } else {
            ele.style.visibility = "hidden";
            iWantDonation.innerHTML = "赏";
        }
    }

}